﻿using Lamex.Base;
using Lamex.Library;
using Lamex.Library.CommonClass;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mNC.Common
{
    public class GetCreoData
    {
        public static void Function()
        {
            #region TXT导入
            //SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
            ////获取当前文件夹下所有的文件
            //string path = @"\\Tx-fs10\tx-fs1\LAMEX\Parametric\FeatData\Aluminum";
            //string pathBack = @"\\Tx-fs10\tx-fs1\LAMEX\NC\FeatData\Aluminum\";
            //DirectoryInfo folder = new DirectoryInfo(path);
            //int isums = 0, icount_true = 0, icount_false = 0;
            //try
            //{
            //    foreach (FileInfo file in folder.GetFiles("*.txt"))
            //    {
            //        isums++;
            //        //判断数据库是否存在，若存在则不允许保存
            //        string sql = @" select [FILE_NAME] FROM [LamexMESManager].[dbo].[NC_PN_AL_FeatData_Creo]
            //                        where [FILE_NAME]='" + file.Name + "'";
            //        if (BaseBuildSql.GetDataTableBySql(sql).Rows.Count > 0)
            //        {
            //            DevExpress.XtraEditors.XtraMessageBox.Show("【" + file.Name + "】文件已存在，不允许重复获取！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //            icount_false++;
            //            continue;
            //        }

            //        DataTable TABLE_CREO = Common.GetTable.GetTable_FromCreo();
            //        string fileName = file.FullName;
            //        //采用流的方式读取
            //        StreamReader sr = new StreamReader(fileName, Encoding.Default);
            //        string nextLine;
            //        while ((nextLine = sr.ReadLine()) != null)
            //        {
            //            DataRow dr = TABLE_CREO.NewRow();
            //            string strList = nextLine.ToString();
            //            string[] str = strList.Split(';');
            //            dr["FILE_NAME"] = file.Name;
            //            dr["DWG_NO"] = str[0];
            //            dr["MODULE_CODE"] = str[1];
            //            dr["PART_NO"] = str[2];
            //            dr["PART_LENGTH"] = str[3];
            //            dr["WORK_SURFACE"] = str[4];
            //            dr["FACE_WIDTH"] = str[5];
            //            dr["FACE_HEIGHT"] = str[6];
            //            dr["UDF_NAME"] = str[7];
            //            dr["FEATURE_TYPE"] = str[8];
            //            dr["DEEP_CODE"] = str[9];
            //            dr["COORD_X"] = str[10];
            //            dr["COORD_Y"] = str[11];
            //            dr["COORD_Z"] = str[12];
            //            dr["FEATURE_DIA"] = str[13].ToUpper() == "NULL" ? null : str[13];
            //            dr["SLOT_LENGTH"] = str[14].ToUpper() == "NULL" ? null : str[14];
            //            dr["SLOT_WIDTH"] = str[15].ToUpper() == "NULL" ? null : str[15];
            //            dr["FILLIT_RADIUS"] = str[16].ToUpper() == "NULL" ? null : str[16];
            //            dr["CREO_OUTPUT_TIME"] = file.CreationTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
            //            dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            //            dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
            //            TABLE_CREO.Rows.Add(dr);
            //        }
            //        //批量插入数据库
            //        if (BaseBuildSql.ExcuteInsert(TABLE_CREO, "NC_PN_AL_FeatData_Creo"))
            //        {
            //            TABLE_CREO = new DataTable();
            //            // 导入成功后移动文件到新的文件夹
            //            File.Copy(fileName, pathBack + file.Name, true);
            //            sr.Close();
            //            File.Delete(fileName);
            //            icount_true++;
            //        }
            //        else
            //        {
            //            icount_false++;
            //        }

            //    }
            //    DevExpress.XtraEditors.XtraMessageBox.Show("共【" + isums + "】个文件，成功【" + icount_true + "】个，失败【" + icount_false + "】个！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    SplashScreenTool.CloseSplashScreen();
            //}
            //catch (Exception ex)
            //{
            //    SplashScreenTool.CloseSplashScreen();
            //    DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
            //}
            #endregion

            #region CSV导入
            SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
            //获取当前文件夹下所有的文件
            string path = @"\\Tx-fs10\tx-fs1\LAMEX\Parametric\FeatData\Aluminum";
            string pathBack = @"\\Tx-fs10\tx-fs1\LAMEX\NC\FeatData\Aluminum\";
            DirectoryInfo folder = new DirectoryInfo(path);
            int isums = 0, icount_true = 0, icount_false = 0;
            try
            {
                foreach (FileInfo file in folder.GetFiles("*.csv"))
                {
                    isums++;
                    //判断数据库是否存在，若存在则不允许保存
                    string sql = @" select [FILE_NAME] FROM [LamexMESManager].[dbo].[NC_PN_AL_FeatData_Creo]
                                    where [FILE_NAME]='" + file.Name + "'";
                    if (BaseBuildSql.GetDataTableBySql(sql).Rows.Count > 0)
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("【" + file.Name + "】文件已存在，不允许重复获取！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        icount_false++;
                        continue;
                    }

                    DataTable TABLE_CREO = Common.GetTable.GetTable_FromCreo();
                    string fileName = file.FullName;
                    //采用流的方式读取
                    DataTable dataTable = Common.CSVHelper.CSVToDataTableByStreamReader(fileName);
                    if (dataTable.Rows.Count > 0)
                    {
                        for (int i = 0; i < dataTable.Rows.Count; i++)
                        {
                            DataRow dr = TABLE_CREO.NewRow();
                            dr["FILE_NAME"] = file.Name;
                            dr["DWG_NO"] = dataTable.Rows[i]["图纸编号"].ToString();
                            dr["MODULE_CODE"] = dataTable.Rows[i]["型材编号"].ToString();
                            dr["PART_NO"] = dataTable.Rows[i]["部件编号"].ToString();
                            dr["PART_LENGTH"] = dataTable.Rows[i]["长度"].ToString();
                            dr["WORK_SURFACE"] = dataTable.Rows[i]["加工面"].ToString();
                            dr["FACE_WIDTH"] = dataTable.Rows[i]["宽度"].ToString();
                            dr["FACE_HEIGHT"] = dataTable.Rows[i]["高度"].ToString();
                            dr["UDF_NAME"] = dataTable.Rows[i]["UDF名称"].ToString();
                            dr["FEATURE_TYPE"] = dataTable.Rows[i]["特征类型"].ToString();
                            dr["DEEP_CODE"] = dataTable.Rows[i]["深度代码"].ToString();
                            dr["COORD_X"] = dataTable.Rows[i]["X坐标"].ToString();
                            dr["COORD_Y"] = dataTable.Rows[i]["Y坐标"].ToString();
                            dr["COORD_Z"] = dataTable.Rows[i]["Z坐标"].ToString();
                            dr["FEATURE_DIA"] = dataTable.Rows[i]["直径"].ToString().ToUpper() == "NULL" ? null : dataTable.Rows[i]["直径"].ToString();
                            dr["SLOT_LENGTH"] = dataTable.Rows[i]["槽长"].ToString().ToUpper() == "NULL" ? null : dataTable.Rows[i]["槽长"].ToString();
                            dr["SLOT_WIDTH"] = dataTable.Rows[i]["槽宽"].ToString().ToUpper() == "NULL" ? null : dataTable.Rows[i]["槽宽"].ToString();
                            dr["FILLIT_RADIUS"] = dataTable.Rows[i]["圆角"].ToString().ToUpper() == "NULL" ? null : dataTable.Rows[i]["圆角"].ToString();
                            dr["FEATURE_DEPTH"] = dataTable.Rows[i]["深度"].ToString().ToUpper() == "NULL" ? null : dataTable.Rows[i]["深度"].ToString();
                            dr["CREO_OUTPUT_TIME"] = file.CreationTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
                            dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                            dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
                            TABLE_CREO.Rows.Add(dr);

                            #region 单条插入
                            //                            string sql11 = @"Insert into [dbo].[NC_PN_AL_FeatData_Creo] values
                            //('"+ dr["FILE_NAME"] + "',	'"+ dr["DWG_NO"] + "',	'"+ dr["MODULE_CODE"] + "',	'"+ dr["PART_NO"] + @"',
                            //	"+ dr["PART_LENGTH"] + ",	'"+ dr["WORK_SURFACE"] + "',	"+ dr["FACE_WIDTH"] + ",	"+ dr["FACE_HEIGHT"] + ",	'"+ dr["UDF_NAME"] + "',	'"+ dr["FEATURE_TYPE"] + "',	'"+ dr["DEEP_CODE"] + @"',
                            //		"+ dr["COORD_X"] + ",	"+ dr["COORD_X"] + ",	"+ dr["COORD_Z"] + ",	"+ dr["FEATURE_DIA"] + ",	"+ dr["SLOT_LENGTH"] + ",	"+ dr["SLOT_WIDTH"] + ",	"+ dr["FILLIT_RADIUS"] + ",	"+ dr["FEATURE_DEPTH"] + @",
                            //			'2020-05-20 15:59:06.218',	'2020-05-20 16:25:40.103',	'TX02869'
                            //)";
                            //                            BaseBuildSql.ExecuteNonQuery(sql11);
                            #endregion
                        }
                    }
                    //批量插入数据库
                    if (BaseBuildSql.ExcuteInsert(TABLE_CREO, "NC_PN_AL_FeatData_Creo"))
                    {
                        TABLE_CREO = new DataTable();
                        // 导入成功后移动文件到新的文件夹
                        File.Copy(fileName, pathBack + file.Name, true);
                        File.Delete(fileName);
                        icount_true++;
                    }
                    else
                    {
                        icount_false++;
                    }

                }
                DevExpress.XtraEditors.XtraMessageBox.Show("共【" + isums + "】个文件，成功【" + icount_true + "】个，失败【" + icount_false + "】个！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SplashScreenTool.CloseSplashScreen();
            }
            catch (Exception ex)
            {
                SplashScreenTool.CloseSplashScreen();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
            }
            #endregion

        }
    }
}
